package com.right.health.app.ui.medical.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magic.basic.adapter.BaseAbsAdapter;
import com.right.health.app.R;
import com.right.health.app.able.ISampleTextWatcher;
import com.right.health.app.app.App;
import com.right.health.app.bean.MedicalRecordInfo;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.utils.GlideUtil;
import com.right.health.app.widget.GridViewEx;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class MedicalRecordAddAdapter extends BaseAbsAdapter<MedicalRecordInfo>  {

    public int mSelectedEditTextPosition = -1;

    private View.OnClickListener mClickListener;


    private OnAppendPhotListener mAppendListener;

    public void setOnClickListener(View.OnClickListener listener) {
        this.mClickListener = listener;
    }

    public MedicalRecordAddAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.medical_record_add_item, null);
            viewHolder = new ViewHolder();

            viewHolder.contentEt = (EditText) convertView.findViewById(R.id.et_content);
            viewHolder.courseDiseaseTv = (TextView) convertView.findViewById(R.id.tv_course_disease);
            viewHolder.delTv = (TextView) convertView.findViewById(R.id.tv_del);

            viewHolder.photoGrid = (GridViewEx) convertView.findViewById(R.id.photo_grid);
            viewHolder.photoAdapter = new PhotoAdapter(mContext);
            viewHolder.photoGrid.setAdapter(viewHolder.photoAdapter);
            viewHolder.photoGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int pos = (int) parent.getTag();
                    MedicalRecordInfo info = mDataSource.get(pos);
                    if (TextUtils.equals(info.photoDataSource.get(position),"add")&& mAppendListener != null) { //点击最后一项为添加
                        mAppendListener.onTap(pos);
                    }
                }
            });

            if (mClickListener != null) {
                viewHolder.courseDiseaseTv.setOnClickListener(mClickListener);
                viewHolder.delTv.setOnClickListener(mClickListener);
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        MedicalRecordInfo info = mDataSource.get(position);
        viewHolder.courseDiseaseTv.setTag(position);
        viewHolder.photoGrid.setTag(position);
        viewHolder.delTv.setTag(position);
        viewHolder.contentEt.setTag(position);
        viewHolder.contentEt.addTextChangedListener(new MyTextWatcher(viewHolder));
        if (TextUtils.isEmpty(info.casehistorydesc)) {
            viewHolder.contentEt.setText("");
        } else {
            viewHolder.contentEt.setText(info.casehistorydesc);
        }
        viewHolder.courseDiseaseTv.setText(info.infectionprogressName);
        viewHolder.photoAdapter.setDataSource(info.photoDataSource);

        return convertView;
    }

    private class ViewHolder {

        private EditText contentEt;
        private TextView courseDiseaseTv;
        private TextView delTv;

        private GridViewEx photoGrid;
        private PhotoAdapter photoAdapter;
    }

    public interface OnAppendPhotListener {
        void onTap(int pos);
    }

    class MyTextWatcher implements TextWatcher {


        private ViewHolder mHolder;

        public MyTextWatcher(ViewHolder holder) {
            mHolder = holder;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            int position = (int) mHolder.contentEt.getTag();


            mDataSource.get(position).casehistorydesc = s.toString();

        }

    }




    private class PhotoAdapter extends BaseAbsAdapter<String> {

        PhotoAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PhotoViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.medical_record_add_photo_item, null);
                viewHolder = new PhotoViewHolder();
                viewHolder.contentImage = (ImageView) convertView.findViewById(R.id.iv_content);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (PhotoViewHolder) convertView.getTag();
            }
            String path = mDataSource.get(position);
            if (TextUtils.equals("add",path)) {
                Glide.with(App.getInstance()).load(R.mipmap.ic_add_photo).into(viewHolder.contentImage);
            } else {
                Glide.with(App.getInstance()).load(AppConfig.PHOTO_URL + path).apply(GlideUtil.getDefaultPlaceholder()).into(viewHolder.contentImage);
            }
            return convertView;
        }

        private class PhotoViewHolder {
            private ImageView contentImage;
        }
    }

    public void setAppendListener(OnAppendPhotListener listener) {
        this.mAppendListener = listener;
    }
}
